package com.lvy.hczn.front.business.service.impl;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lvy.hczn.front.common.exception.UtilException;
import com.lvy.hczn.front.system.domain.business.Shelves;
import com.lvy.hczn.front.business.mapper.ShelvesMapper;
import com.lvy.hczn.front.business.service.ShelvesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.lvy.hczn.front.common.constant.Constants;

import java.math.BigDecimal;

@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class ShelvesServiceImpl extends ServiceImpl<ShelvesMapper, Shelves> implements ShelvesService {

    /**
     * 物理货位定时任务初始化，理论一小时初始化一次，因为要配送，需整点分拣完
     * 最好的方式是，配送员取货触发，目前平台未开发对接，暂时定时
     *
     * @param volume
     * @return void
     * @author yw
     * @date 2023-12-20 15:25:33
     */
    @Override
    public void initShelvesTask(String volume) {
        if (StrUtil.isEmpty(volume)) {
            throw new UtilException("参数为空");
        }
        if (!NumberUtil.isNumber(volume)) {
            throw new UtilException("参数为非数字");
        }
        LambdaUpdateWrapper<Shelves> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Shelves::getOccupied, Constants.OCCUPIED_TRUE);
        updateWrapper.isNotNull(Shelves::getSaleOrderCode);
        updateWrapper.isNotNull(Shelves::getDeliveryClerk);
        updateWrapper.set(Shelves::getVolume, new BigDecimal(volume));
        updateWrapper.set(Shelves::getOccupied, Constants.OCCUPY_FALSE);
        updateWrapper.set(Shelves::getSaleOrderCode, null);
        updateWrapper.set(Shelves::getDeliveryClerk, null);
        super.update(updateWrapper);
    }
}
